package com.yequan.thread.countdownlatch;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author yq
 * @date 2020/09/03
 */
public class CountDownLatchDemo3 {

    private static CountDownLatch latch = new CountDownLatch(3);
    private static AtomicInteger num = new AtomicInteger(0);

    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(() -> {
            int andAdd = num.addAndGet(1);
            System.out.println("线程1完成工作：" + andAdd);
            latch.countDown();
        });
        Thread thread2 = new Thread(() -> {
            int andAdd = num.addAndGet(2);
            System.out.println("线程2完成工作：" + andAdd);
            latch.countDown();
        });
        Thread thread3 = new Thread(() -> {
            int andAdd = num.addAndGet(3);
            System.out.println("线程3完成工作：" + andAdd);
            latch.countDown();
        });
        Thread thread4 = new Thread(() -> {
            System.out.println("线程4汇总：" + num.get());
            latch.countDown();
        });
        thread1.start();
        thread2.start();
        thread3.start();
        latch.await();
        thread4.start();
    }

}
